package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.Recharge;
import com.sise.parkinglot.mapper.RechargeMapper;
import com.sise.parkinglot.service.IRechargeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, Recharge> implements IRechargeService {

    @Resource
    private RechargeMapper rechargeMapper;

    @Override
    public CodeMsg<List<Recharge>> queryRechargeList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<Recharge>>().setCode(400).setMsg("参数缺失");
        }
        Page<Recharge> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<Recharge> rechargeQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            rechargeQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("userId") != null && !map.get("userId").equals("")) {
            rechargeQueryWrapper.eq("user_id", map.get("userId"));
        }
        if (map.get("source") != null && !map.get("source").equals("")) {
            rechargeQueryWrapper.eq("source", map.get("source"));
        }
        if (map.get("recharge") != null && !map.get("recharge").equals("")) {
            rechargeQueryWrapper.like("recharge", map.get("recharge"));
        }
        if (map.get("msg") != null && !map.get("msg").equals("")) {
            rechargeQueryWrapper.like("msg", map.get("msg"));
        }
        if (map.get("createTime") != null && !map.get("createTime").equals("")) {
            String createTime = (String) map.get("createTime");
            rechargeQueryWrapper.likeRight("create_time", createTime.split(" ")[0]);
        }
        rechargeQueryWrapper.lambda().orderByDesc(Recharge::getCreateTime);
        IPage<Recharge> rechargePage = rechargeMapper.selectPage(page, rechargeQueryWrapper);
        List<Recharge> rechargeList = rechargePage.getRecords();
        return new CodeMsg<List<Recharge>>().setCode(200).setMsg(String.valueOf(rechargePage.getTotal())).setData(rechargeList);
    }
}
